package cn.shengongying.admin.controller;

import cn.shengongying.admin.dto.BannerListInDTO;
import cn.shengongying.bean.DataTablePageResult;
import cn.shengongying.bean.ResponseResult;
import cn.shengongying.model.BannerList;
import cn.shengongying.service.IBannerListService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * author: RXK
 * date: 2022/2/19 星期六
 * desc:
 */
@Controller
@RequestMapping("/sys/banner")
public class BannerController extends BaseController {


    private final IBannerListService bannerListService;

    public BannerController(IBannerListService bannerListService) {
        this.bannerListService = bannerListService;
    }


    @RequestMapping("/index")
    public String index() {
        return "/banner/index";
    }

    @ResponseBody
    @RequestMapping("/list")
    public DataTablePageResult<List<BannerList>> list(@RequestBody BannerListInDTO inDTO) {
        List<BannerList> list = bannerListService.list();
        inDTO.setData(list);
        long count = bannerListService.count();
        inDTO.setRecordsTotal(count);
        return inDTO;
    }


    @ResponseBody
    @RequestMapping("/detail")
    public ResponseResult<BannerList> detail(@RequestBody BannerListInDTO inDTO) {
        BannerList bannerList = bannerListService.getById(inDTO.getId());
        return ResponseResult.success(bannerList);
    }

    @ResponseBody
    @RequestMapping("/save")
    public ResponseResult<String> save(BannerListInDTO inDTO, HttpServletRequest request) {
        BannerList list = inDTO.bannerList();
        try {
            String filePath = multiFile(request);
            list.setImage(filePath);
        } catch (Exception e) {
            logger.error("上传文件出现异常",e);
            return ResponseResult.fail();
        }
        bannerListService.saveOrUpdate(list);
        return ResponseResult.success();
    }


}
